//
//  LoginView.swift
//  XMGG
//
//  Created by iDev on 6/30/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit

class LoginView: UIView, UITextFieldDelegate {

    private let imageView = UIImageView()
    private let phoneImageView = UIImageView()
    private let pwdImageView = UIImageView()
    private let phoneInput = UITextField()
    private let pwdInput = UITextField()
    private let forgetPwd = UIButton()
    
    private let loginBtn = UIButton()
    private let registerBtn = UIButton()

    private var username:String?
    
//    var delegate:LoginViewDelegate?

    init(frame: CGRect, name:String?) {
        super.init(frame: frame)
        username = name
        setupView()
        setupLayout()
        let gesture = UISwipeGestureRecognizer(target: self, action: #selector(swipup))
        gesture.direction = .up
        self.addGestureRecognizer(gesture)
    }
    
    func setupView(){
        addSubview(imageView)
        imageView.layer.cornerRadius = 42.5
        imageView.image = UIImage(named: "content_iconpeople")
        imageView.backgroundColor = UIColor(patternImage: UIImage(named: "content_iconshadow")!)
        
        addSubview(phoneImageView)
        phoneImageView.image = UIImage(named: "column_iconphone")
        
        addSubview(phoneInput)
        phoneInput.text = username
        phoneInput.delegate = self
        phoneInput.attributedPlaceholder = NSAttributedString(string: "请输入手机号码", attributes: [NSForegroundColorAttributeName:UIColor(red: 112/255.0, green: 112/255.0, blue: 112/255.0, alpha: 1.0),NSFontAttributeName:UIFont(name: "PingFang SC", size: 25*0.75)!])
        phoneInput.font = UIFont(name: "PingFang SC", size: 25*0.75)
        phoneInput.keyboardType = .numberPad
        
        addSubview(pwdImageView)
        pwdImageView.image = UIImage(named: "column_iconlock")

        addSubview(pwdInput)
        pwdInput.delegate = self
        pwdInput.attributedPlaceholder = NSAttributedString(string: "请输入密码", attributes: [NSForegroundColorAttributeName:UIColor(red: 112/255.0, green: 112/255.0, blue: 112/255.0, alpha: 1.0),NSFontAttributeName:UIFont(name: "PingFang SC", size: 25*0.75)!])
        pwdInput.font = UIFont(name: "PingFang SC", size: 25*0.75)
        pwdInput.isSecureTextEntry = true
        pwdInput.contentVerticalAlignment = .center
        
        addSubview(forgetPwd)
        forgetPwd.setTitleColor(TEXTGREEN, for: .normal)
        let attrTitle = NSAttributedString(string: "忘记密码？", attributes: [NSForegroundColorAttributeName:UIColor(red: 46/255.0, green: 100/255.0, blue: 100/255.0, alpha: 1.0),NSUnderlineStyleAttributeName:1,NSFontAttributeName:UIFont(name: "PingFang SC", size: 18*0.75)!])
        forgetPwd.setAttributedTitle(attrTitle, for: .normal)
        forgetPwd.sizeToFit()
        
        addSubview(loginBtn)
        loginBtn.setBackgroundImage(colorImg(color: UIColor(red: 56/255.0, green: 124/255.0, blue: 124/255.0, alpha: 1.0)), for: .normal)
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.layer.cornerRadius = 3
        loginBtn.layer.masksToBounds = true
        loginBtn.addTarget(LoginViewController.self(), action: NSSelectorFromString("userRequestLogin"), for: .touchUpInside)
        
        addSubview(registerBtn)
        registerBtn.setBackgroundImage(colorImg(color: UIColor(red: 56/255.0, green: 124/255.0, blue: 124/255.0, alpha: 1.0)), for: .normal)
        registerBtn.setTitle("注册", for: .normal)
        registerBtn.layer.cornerRadius = 3
        registerBtn.layer.masksToBounds = true
        registerBtn.addTarget(LoginViewController.self(), action: NSSelectorFromString("registerBtnClick"), for: .touchUpInside)
    }
    
    func setupLayout(){
        imageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(70)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(85)
        }
        let line0 = makeLine()
        addSubview(line0)
        line0.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(46)
            make.bottom.equalTo(imageView.snp.bottom).offset(68)
            make.right.equalToSuperview().offset(-46)
            make.height.equalTo(1)
        }
        
        phoneImageView.snp.makeConstraints { (make) in
            make.left.equalTo(line0.snp.left).offset(14.5)
            make.bottom.equalTo(line0.snp.bottom).offset(-6.5)
            make.width.height.equalTo(20)
        }

        let line00 = makeLine()
        addSubview(line00)
        line00.snp.makeConstraints { (make) in
            make.left.equalTo(line0).offset(49)
            make.centerY.equalTo(phoneImageView)
            make.width.equalTo(1)
            make.height.equalTo(phoneImageView.snp.height)
        }
        
        phoneInput.snp.makeConstraints { (make) in
            make.left.equalTo(line00.snp.right).offset(16)
            make.right.equalToSuperview().offset(-46)
            make.top.equalTo(line00).offset(-10)
            make.bottom.equalTo(line00).offset(10)
        }
        let line1 = makeLine()
        addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(line0)
            make.bottom.equalTo(line0.snp.top).offset(55)
            make.right.equalTo(line0)
            make.height.equalTo(line0)
        }

        pwdImageView.snp.makeConstraints { (make) in
            make.left.equalTo(phoneImageView)
            make.top.equalTo(line0.snp.bottom).offset(28)
        }
        let line10 = makeLine()
        addSubview(line10)
        line10.snp.makeConstraints { (make) in
            make.left.equalTo(line1).offset(49)
            make.centerY.equalTo(pwdImageView)
            make.width.height.equalTo(line00)
        }
        
        pwdInput.snp.makeConstraints { (make) in
            make.left.right.equalTo(phoneInput)
            make.top.equalTo(line10).offset(-10)
            make.bottom.equalTo(line10).offset(10)
        }
        forgetPwd.snp.makeConstraints { (make) in
            make.top.equalTo(pwdInput.snp.bottom).offset(18)
            make.right.equalTo(phoneInput).offset(-4)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(forgetPwd).offset(37)
            make.left.right.equalTo(line0)
            make.height.equalTo(33)
        }
        registerBtn.snp.makeConstraints { (make) in
            make.top.equalTo(loginBtn.snp.bottom).offset(23)
            make.left.right.height.equalTo(loginBtn)
        }
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        firstRespondView = textField
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        firstRespondView?.resignFirstResponder()
        return true
    }
    
    func getLoginModle()->LoginModle{
        let modle = LoginModle(tu_account: phoneInput.text, tu_password: pwdInput.text, tu_loginDate: getCurrentTime())
        return modle
    }
    
    func swipup(){
        firstRespondView?.resignFirstResponder()
    }
    
    private func makeLine()->UIView {
        let sepreateLine = UIView()
            sepreateLine.backgroundColor = UIColor(red: 201/255.0, green: 201/255.0, blue: 201/255.0, alpha: 1.0)
        return sepreateLine
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//protocol LoginViewDelegate {
//    func loginBtnClick(modle:LoginModle)
//    func registerBtnClick()
//}

